

import java.util.ArrayList;
import java.util.PriorityQueue;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

//@author A0096724W
public class InternalMemory {
    private static ObservableList<Task> eventItems =FXCollections.observableArrayList ();
    private static ObservableList<Task> deadlineItems =FXCollections.observableArrayList ();
    private static ObservableList<Task> miscItems =FXCollections.observableArrayList ();

    public static int setTask(PriorityQueue<Task> pq, String type) {
	ObservableList<Task> ol = null;

	int indexOfNewItem = -1;

	ol = typeToList(type);

	if (ol != null) {
	    int i = 1;
	    PriorityQueue<Task> temp = new PriorityQueue<Task>();
	    
	    //copy contents to temporary queue
	    while (!pq.isEmpty()){
		Task t = pq.poll();
		t.setIndex(i);
		
		//If this item was recently modified, take note of index to select in GUI
		if (t.isJustEdited()) {
		    indexOfNewItem = i;
		    t.setJustEdited(false);
		}
		temp.add(t);
		ol.add(t);
		i++;
	    }
	    
	    //add the items back to original queue
	    while (!temp.isEmpty()) {
		pq.add(temp.poll());
	    }
	}

	return indexOfNewItem;
    }

    private static ObservableList<Task> typeToList(String type) {
	if (type.equals("event")) {
	    eventItems.clear();
	    return eventItems;
	}

	if (type.equals("deadline")) {
	    deadlineItems.clear();
	    return deadlineItems;
	}

	if (type.equals("misc")) {
	    miscItems.clear();
	    return miscItems;
	}
	return null;
    }

    public static void refreshAllList() {
	ArrayList<Task> tempArray = new ArrayList<Task>();

	refreshEventList(tempArray);
	refreshDeadlineList(tempArray);
	refreshMiscList(tempArray);
    }

    private static void refreshMiscList(ArrayList<Task> tempArray) {
	copyMiscList(tempArray);
	miscItems.clear();
	putBackMiscItems(tempArray);
    }

    private static void putBackMiscItems(ArrayList<Task> tempArray) {
	for (int i = 1; i <= tempArray.size(); i++) {
	    Task t = tempArray.get(i - 1);
	    t.setIndex(i);
	    miscItems.add(t);
	}
    }

    private static void copyMiscList(ArrayList<Task> tempArray) {
	for (int i = 1; i <= miscItems.size(); i++) {
	    Task t = miscItems.get(i - 1);
	    tempArray.add(t);
	}
    }

    private static void refreshDeadlineList(ArrayList<Task> tempArray) {
	copyDeadlineList(tempArray);
	deadlineItems.clear();
	putBackDeadlineItems(tempArray);
	tempArray.clear();
    }

    private static void putBackDeadlineItems(ArrayList<Task> tempArray) {
	for (int i = 1; i <= tempArray.size(); i++) {
	    Task t = tempArray.get(i - 1);
	    t.setIndex(i);
	    deadlineItems.add(t);
	}
    }

    private static void copyDeadlineList(ArrayList<Task> tempArray) {
	for (int i = 1; i <= deadlineItems.size(); i++) {
	    Task t = deadlineItems.get(i - 1);
	    tempArray.add(t);
	}
    }

    private static void refreshEventList(ArrayList<Task> tempArray) {
	copyEventList(tempArray);
	eventItems.clear();
	putBackEventItems(tempArray);
	tempArray.clear();
    }

    private static void putBackEventItems(ArrayList<Task> tempArray) {
	for (int i = 1; i <= tempArray.size(); i++) {
	    Task t = tempArray.get(i - 1);
	    t.setIndex(i);
	    eventItems.add(t);
	}
    }

    private static void copyEventList(ArrayList<Task> tempArray) {
	for (int i = 1; i <= eventItems.size(); i++) {
	    Task t = eventItems.get(i - 1);
	    tempArray.add(t);
	}
    }

    public static ObservableList<Task> getEventTask() {
	return eventItems;
    }

    public static ObservableList<Task> getDeadlineTask() {
	return deadlineItems;
    }

    public static ObservableList<Task> getMiscTask() {
	return miscItems;
    }
}
